//
//  BaseSegContainerVC.m
//  RuiBeiKang
//
//  Created by 江伟 on 2018/11/24.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "BaseSegContainerVC.h"

@interface BaseSegContainerVC ()<JXCategoryViewDelegate>
@property (nonatomic, strong) NSMutableArray *listVCArray;
@end

@implementation BaseSegContainerVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _defaultSelectIndex = 0;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.listVCContainerView parentVCWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
    [self.listVCContainerView parentVCDidAppear:animated];
}

//这句代码必须加上
- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.listVCContainerView parentVCWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.listVCContainerView parentVCDidDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (self.navigationController.viewControllers.count > 0) {
        [self setNavLeftBtnWithImageName:@"back"];
    }
    // Do any additional setup after loading the view.
}

- (void)onTapNavLeftBtn {
    [[NSNotificationCenter defaultCenter] postNotificationName:SegContainerVCBackNotification object:nil];
    [self back];
}

- (void)setupViews {
    [self.view addSubview:self.categoryView];
    self.listVCArray = [self viewControllers].mutableCopy;
    self.categoryView.titleColor = textColor1;
    self.categoryView.titleSelectedColor = mainColor;
    self.listVCContainerView.listVCArray = self.listVCArray;
    [self.view addSubview:self.listVCContainerView];
    self.categoryView.contentScrollView = self.listVCContainerView.scrollView;
    self.lineView.indicatorLineViewColor = mainColor;
    self.lineView.indicatorLineWidth = 70;
}

/**
 重载数据源：比如从服务器获取新的数据、否则用户对分类进行了排序等
 */
- (void)reloadData {
    NSArray *titles = [self titles];
    //先把之前的listView移除掉
    for (UIViewController *vc in self.listVCArray) {
        [vc.view removeFromSuperview];
    }
    [self.listVCArray removeAllObjects];
    
    self.listVCArray = [self viewControllers].mutableCopy;
    
    for (int i = 0; i < self.listVCArray.count; i ++) {
        UIViewController *vc = self.listVCArray[i];
        vc.view.frame = CGRectMake(i * KWidth, 0, KWidth, ViewHeight3 - aufont(40));
    }
    
    self.listVCContainerView.listVCArray = self.listVCArray;
    self.listVCContainerView.defaultSelectedIndex = 0;
    [self.listVCContainerView reloadData];
    
    //重载之后默认回到0，你也可以指定一个index
    self.categoryView.defaultSelectedIndex = 0;
    self.categoryView.titles = titles;
    [self.categoryView reloadData];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listVCContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    [self.listVCContainerView didScrollSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listVCContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio];
}

- (NSArray *)titles {
    return @[];
}

- (NSArray *)viewControllers {
    return @[];
}

- (void)sc_setupViews {}

- (NSMutableArray *)listVCArray {
    if(!_listVCArray) {
        _listVCArray = [NSMutableArray array];
    }
    return _listVCArray;
}

- (JXCategoryIndicatorLineView *)lineView {
    if (!_lineView) {
        _lineView = [[JXCategoryIndicatorLineView alloc] init];
    }
    return _lineView;
}

- (JXCategoryTitleView *)categoryView {
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.frame = CGRectMake(0, 0, KWidth, aufont(40));
        _categoryView.delegate = self;
        _categoryView.titles = [self titles];
        _categoryView.indicators = @[self.lineView];
        _categoryView.defaultSelectedIndex = self.defaultSelectIndex;
    }
    return _categoryView;
}

- (JXCategoryListVCContainerView *)listVCContainerView {
    if (!_listVCContainerView) {
        _listVCContainerView = [[JXCategoryListVCContainerView alloc] initWithFrame:CGRectMake(0, aufont(40), KWidth, ViewHeight1  - aufont(40))];
        _listVCContainerView.defaultSelectedIndex = self.defaultSelectIndex;
    }
    return _listVCContainerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
